﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

namespace S7.Windows.Music
{
    /// <summary>
    /// Interaction logic for ChangeLibraryLocation.xaml
    /// </summary>
    public partial class ChangeLibraryLocation : Window
    {
        public ChangeLibraryLocation()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            tbLocation.Text = S7.Properties.Settings.Default.MediaPath;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (!tbLocation.Text.Equals(""))
            {
                string path = tbLocation.Text;
                Thread thr = new Thread(new ThreadStart(() =>
                    {
                        S7.Music.LoadMusic(path);
                        S7.Properties.Settings.Default.MediaPath = path;
                        Properties.Settings.Default.Save();
                        MessageBox.Show("Media's path is changed!");
                        this.Dispatcher.Invoke(new ThreadStart(() =>
                            {
                                this.Close();
                            }));
                    }));
                thr.IsBackground = true;
                thr.Start();
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog ChooseFolder = new System.Windows.Forms.FolderBrowserDialog();
            System.Windows.Forms.DialogResult dg = ChooseFolder.ShowDialog();
            if (dg == System.Windows.Forms.DialogResult.OK)
            {
                tbLocation.Text = ChooseFolder.SelectedPath;
            }
        }
    }
}
